package com.pcxm.cm.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.pcxm.cm.pojo.SysUser;

public class CheckSession implements Filter {

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		System.out.println("----过滤器初始化----");
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		// 对request和response进行一些预处理
		req.setCharacterEncoding("UTF-8");
		res.setCharacterEncoding("UTF-8");
		res.setContentType("text/html;charset=UTF-8");

		System.out.println("FilterDemo01执行前！！！");

		SysUser user = (SysUser) req.getSession().getAttribute("sysuser");
		if (user == null) {
			
			request.getRequestDispatcher("/login.jsp").forward(
					request, response);
			
/*			request.getRequestDispatcher("/public-pcxm/mainPage.action").forward(
					request, response);*/

		} else {
			// 已登录，放行
			chain.doFilter(request, response);
		}
		System.out.println("FilterDemo01执行后！！！");
	}

	@Override
	public void destroy() {
		System.out.println("----过滤器销毁----");
	}
}